package itsos.controller;


import itsos.model.*;
import itsos.service.*;

import java.sql.Timestamp;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;




public class MemberController extends MultiActionController {
	private static final boolean String = false;
	private LoginService loginService;
	private MemberService memberService;
	private MainIssueService mainIssueService;
	private RepositoryService repositoryService;
	private IssueBoxService issueBoxService;
	

	
	public void setIssueBoxService(IssueBoxService issueBoxService) {
		this.issueBoxService = issueBoxService;
	}
	
	
	public void setRepositoryService(RepositoryService repositoryService)
	{
		this.repositoryService = repositoryService;
	}
	
	public void setLoginService(LoginService loginService)
	{
		this.loginService = loginService;
	}
	
	public void setMemberService(MemberService memberService)
	{
		this.memberService = memberService;
	}
	
	public void setMainIssueService(MainIssueService mainIssueService)
	{
		this.mainIssueService = mainIssueService;
	}
	
	public ModelAndView goLogin(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/mainissue/introPage");
		
		if(!loginService.doLogin(request))
		{
			mav.setViewName("workpage/mainissue/loginErr");			
		}
	
		
		
		//mav.addObject("mainIssue", mainIssueService.viewMainIssue(request, session));
		
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		
		return mav;
	}
	
	public ModelAndView goLogout(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/mainissue/introPage");
		
		HttpSession session = request.getSession();
				
		if((Long)session.getAttribute("memberID")!=null)
		{
			session.invalidate();
		}
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	public ModelAndView addMember(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/member/addMember");
		
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("treeView", repositoryService.getRepository());
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
			mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	
	public ModelAndView insertMember(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/member/addMemberCom");
		
		
		if(!memberService.addUser(request))
		{
			mav.setViewName("workpage/member/nameerr");
			
		}
		
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView updateMember(HttpServletRequest request, HttpServletResponse response, HttpSession session)
	{
		ModelAndView mav = new ModelAndView();
		
		
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	
}
